package com.example.rabbitmqbootconsumer.receiver.direct;

import com.example.rabbitmqbootconsumer.dto.MessageDTO;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import static com.example.rabbitmqbootconsumer.constant.MessageConstant.EXPIRED_QUEUE_NAME;

/**
 * 监听带过期时间的队列
 *
 * @author 伯路平
 * @since 2023/6/23
 **/
@RabbitListener(queues = EXPIRED_QUEUE_NAME)
@Slf4j
@Component
public class DirectExpiredReceiver {
    private static final Gson GSON = new Gson();

    @RabbitHandler
    public void process(String message) {
        try {
            Thread.sleep(1000);
            MessageDTO messageDTO = GSON.fromJson(message, MessageDTO.class);
            log.info("消费者接收到带有过期时间属性的队列的消息:{}", messageDTO);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}
